#include "list.h"

node *list_init()
{
     node *ptr = malloc(sizeof(node));
     if (ptr == NULL)
          return NULL;
     ptr->next = NULL;
     return ptr;
}
void list_create(node *ptr, int *a, int n)
{
     node *p = ptr;
     for (int i = 0; i < n; i++)
     {
          node *new = malloc(sizeof(node));
          if (new == NULL)
               exit(1);
          new->next = NULL;
          new->data = a[i];
          p->next = new;
          p = new;
     }
}
void list_show(node *ptr)
{
     node *p = ptr;
     while (p->next != NULL)
     {
          p = p->next;
          printf("%d  ", p->data);
     }
     printf("\n");
}

void merge(node *ha,node *hb)
{
     node *pa = ha->next;
     node *pb = hb->next;
     node *p = ha, *q = NULL;
     while (pa != NULL && pb != NULL)
     {
          if (pa->data > pb->data)
          {
               q = pb;
               pb = pb->next;
               p->next = q;
               q->next = pa;
               p = p->next;
          }
          else
          {
               pa = pa->next;
               p = p->next;
          }
     }
     if(pa == NULL)
          p->next = pb;
}
void turn(node *ha)
{
     node *pa = ha->next;
     node *tail = NULL;
     node *current = ha;

     while (current->next != NULL)
          current = current->next;

     tail = current;

     node * head = pa;

     while(head != tail)
     {
          current = head;
          head = head->next;
          current->next = tail->next;
          tail->next = current;
     }
     ha->next = tail;
}